/*
 * CadastrodeTipodeProduto.java
 *
 * Created on 26 de Novembro de 2008, 19:26
 */
package br.faimg.pomar.visual;

import br.faimg.pomar.controle.BusinessDelegate.TipodeProdutoBusinessDelegate;

/**
 *
 * @author  diogo
 */
public class CadastrodeTipodeProdutoInclusao extends javax.swing.JDialog {

    /** Creates new form CadastrodeTipodeProduto */
    public CadastrodeTipodeProdutoInclusao() {
        initComponents();

    }
    
    public CadastrodeTipodeProdutoInclusao(String cod, String desc){
        initComponents();
               
        codigoTextField.setText(cod);
        descricaoTextField.setText(desc);
                
        okButton.setText("Alterar");
        cancelarButton.setText("Limpar");
        codigoTextField.setEditable(false);
        jLabel1.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        codigoTextField = new javax.swing.JTextField();
        descricaoTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelarButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        erroEntradaDados = new javax.swing.JLabel();

        setTitle("Cadastro de tipo de produto");

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        codigoTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codigoTextFieldActionPerformed(evt);
            }
        });
        codigoTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                codigoTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(codigoTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 20, 40, -1));

        descricaoTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descricaoTextFieldActionPerformed(evt);
            }
        });
        descricaoTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                descricaoTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(descricaoTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 500, -1));

        jLabel2.setText("Descrição: ");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, -1, 20));

        jLabel1.setText("Codigo: ");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 20, -1, 20));

        okButton.setText("Incluir");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel1.add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 80, 80, -1));

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 80, 90, -1));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 580, 10));

        erroEntradaDados.setForeground(new java.awt.Color(204, 0, 0));
        erroEntradaDados.setText("..");
        jPanel1.add(erroEntradaDados, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 20, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 634, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void codigoTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codigoTextFieldActionPerformed
// TODO add your handling code here:
//GEN-LAST:event_codigoTextFieldActionPerformed

}                                               

private void descricaoTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descricaoTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_descricaoTextFieldActionPerformed

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

    if(okButton.getText().equals("Alterar")){
        
        
        if(codigoTextField.getText().equals("")||descricaoTextField.getText().equals("")){
    erroEntradaDados.setText("Entrada inválida!  Existem campos não preenchidos. ");
    
}
else{
    TipodeProdutoBusinessDelegate delegate = new TipodeProdutoBusinessDelegate();
    delegate.update(codigoTextField.getText(), descricaoTextField.getText());
    
    codigoTextField.setEnabled(true);
    cancelarButton.setText("Cancelar");
    

    
    codigoTextField.setText("");
    descricaoTextField.setText("");
    
    okButton.setEnabled(true);
}  
        
    }else{
       if(codigoTextField.getText().equals("")||descricaoTextField.getText().equals("")){
        erroEntradaDados.setText("Entrada inválida!  Existem campos não preenchidos. ");
    }else{
        TipodeProdutoBusinessDelegate delegate = new TipodeProdutoBusinessDelegate();
        delegate.create(codigoTextField.getText(), descricaoTextField.getText());
  

        //Limpa os combos apos a insercao para permitir nova insercao
        codigoTextField.setText("");
        descricaoTextField.setText("");
        cancelarButton.setText("Cancelar"); 
    }
    
    
    
    
    }//GEN-LAST:event_okButtonActionPerformed
    this.setVisible(false);   
}                                        

private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
    if (codigoTextField.getText().equals("") && descricaoTextField.getText().equals("")) {
        this.setVisible(false);
    } else {

        if(okButton.getText().equals("Incluir")){
            codigoTextField.setText("");
            descricaoTextField.setText("");
            codigoTextField.setEnabled(true);
            cancelarButton.setText("Cancelar");
            okButton.setEnabled(true);
            erroEntradaDados.setText("");   
        } else{
            if(descricaoTextField.getText().equals("")){
               this.setVisible(false); 
            }
            codigoTextField.setEnabled(false);
            cancelarButton.setText("Cancelar");
            descricaoTextField.setText("");
            okButton.setEnabled(true);
            erroEntradaDados.setText("");
        }


    }
}//GEN-LAST:event_cancelarButtonActionPerformed

private void codigoTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_codigoTextFieldKeyPressed
// TODO add your handling code here:
    
    cancelarButton.setText("Limpar");
    erroEntradaDados.setText("");
}//GEN-LAST:event_codigoTextFieldKeyPressed

private void descricaoTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descricaoTextFieldKeyPressed
// TODO add your handling code here:
    
    cancelarButton.setText("Limpar");
    erroEntradaDados.setText("");
}//GEN-LAST:event_descricaoTextFieldKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelarButton;
    private javax.swing.JTextField codigoTextField;
    private javax.swing.JTextField descricaoTextField;
    private javax.swing.JLabel erroEntradaDados;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
}
